Computational Thinking

TOPIC 1 - What is computer hardware?

Digital Electronics

Silicon chip

Multicore processors

Moore's law

Moore's law says that we will double transistor capacity every 18-24 months. And this seems to be holding at least for now. Very soon we will have transistors the size of atoms and you can't get smaller than that.

Gates

See "Digital electronics" module for more info, as I don't want to repeat my notes.

Half adder and full adder

To compute the sum of binary numbers we need an adder. We'll first look at the half adder then extend to the full adder.

Half Adder: This sums two binary inputs and creates a carry, if the inputs are 1 and 1. It does not except a carry as an input

Half adder circuit ,theory and working. Truth table , schematic realization

Full Adder: Like a half adder but also excepts a carry as an input and will add that to the sum

full adder schenatic

full adder, half adder realization

Summing multi-digit binary numbers

Now that we have a full adder the simplest way of summing two multi digit binary numbers is to create a chain of full adders where the sum of each digit is computed with the carry from the previous digit sum.

Machine Architecture

Pentium 4 processor microarchitecture

Von Neumann Architecture

This is the fundamental computer architecture and the key idea is the data and program are stored in the same place. This allows for cool self modifying programs. 

Von Neumann Bottleneck: Limitation of the data transfer rate between CPU and memory. This was "fixed" by adding CPU (on-chip) caches, though Harvard architecture was the better fix.

Von Neumann Architecture - Computer Science GCSE GURU

Harvard architecture

This is another architecture where the data memory and instruction memory are stored separately, with separate buses for collecting the data. This solves the Van Neumann Bottleneck but means you can't have self modifying programs. However these days our computers use modified harvard architecture which is more complex and allows the processor to treat data as instructions and instructions as a data hence self modifying programs are again possible. We also kept the CPU caches from harvard architecture.

Memory

Buses

There are 3 types of buses, each used for a different purpose.

Types of memory

TypeDescriptionCostSpeed
Hard DriveData stored off CPU, either on a rotating disk (HDD) or in semiconductor cells (SDD)Cheap to createFar from Cpu this makes it slow.C
Dynamic RAMData stored using transistors/capacitorsCheapSlow compared to static ram. Also capacitors must be refreshed every few seconds.
Static RAMData stored in flipflops, near CPU.Expensive and takes up more space.Stable and fast
Caches

Memory stored very close to CPU, as a buffer between the RAM and CPU. Stores frequently accessed items.

Divided into two levels:

  • Level 1: Stored on CPU (registers, small space)
  • Level 2: On or close to CPU (Cache), large space.
Expensive.

Very fast.

Level 1 is faster than level 2.

Simple Von Neumann Processor

For more information review your "machine architecture" notes.

Multi-core Computing

General purpose graphic processing units (GPGPU)

Instruction Set Architecture and MIPS

Instruction set architecture (ISA)

The instruction set architecture is basically the design of the instructions which allows the programmer to control the hardware. Alot of specific design choices are considered when making an instruction set architecture.

The ISA describes the:

MIPS assembly language

This next part is going to overlap alot with machine architecture so i'll just touch on the essentials.

MIPS assumes:

Some examples of common MIPS assembly instructions:

instructionopcode and fieldsMeaning
Load wordLW $t1 , ({offset}) $sLoads a word from the source + offset to destination
Add intermediateADDI $t1, $s1, immLoads a word from Source adds the intermediate and puts in destination
Branch EqualBEQ $s1, $s2, labelIf source 1 == source 2, increase PC by the PC offset + 4
Set less thanSLT $t1, $s1, $s2If source 1 < Source 2, set Dest to 1 else 0.
JumplabelGo to the target address
HaltHLTStops the program

Types of MIPS instructions

When making an instruction architecture you want a way of standardising the format of the instructions to reduce the circuit complexity, and make programming easier. MIPS is a RISC language which means it has very few instructions and only 3 formats.

Clarification on R, I, and J type Instruction formats in MIPS - Electrical  Engineering Stack Exchange

TypeFormatExample instructions
I-Type (Intermediate and register)

(Opcode, Rs, Rd, Imm)

Bits: 6, 5, 5, 16

  • Opcode = instruction code
  • Rs = Source register
  • Rd = Destination Register
  • imm = memory address or intermediate constant
  • addi
  • beq
  • lw
R-Type (Values all in registers)

(Opcode, Rs1, Rs2, Rd, shift, funct)

Bits: 6, 5, 5, 5, 5, 6

  • Opcode = instruction code (000000)
  • Rs1 = Source register 1
  • Rs2 = Source register 2
  • Rd = Destination Register
  • Shift = Shift value for shift instructions
  • Funct = Supplement for opcode
  • slt
J-Type (jump instructions)

(Opcode, Address)

Bits: 6, 26

  • Opcode = Instruction code
  • Address = Location to jump to
  •  j

 

Operating systems

What are operating systems?

The operating system is the interface between the computer user and the computer hardware.

Kernel: Main component of OS, which loaded at boot time. It has full control of the CPU.

The main functions of the OS are:

Input/Output

Examples of Input/output devices: Hard disk, CD, Graphics card, sound card, ethernet card, modem

These are connected to the cpu via a bus. However Buses are slow and if we had to pause the execution of the whole cpu to wait for an input to be received or output to be sent, it would causes a bottleneck. To solve this the operating system has Interrupts. 

Interrupts: A signal to the cpu that input or output device has sent something on the bus. An interrupt handler announces thse signals to the CPU so that it can process it, then it resumes the paused process.

Processes

These are programs currently being executed. They consist of one or more thread(s) of instructions and an Address Space

Mutual exclusion: A property of processes which is says that no two threads can run the critical section at the same time. Where the critical section is a part of the program which accesses the same shared resource.

You can imagine a scenario where two threads had access to the same counter variable and changed it in some way. But if they ran at the same time one thread would change the counter accessed by the other thread causing all sorts of problems. This is why there must be mutual exclusion

Process life-cycle

The life cycle of a process is quite intuitive. It gets created, it waits for it's turn to run, it's gets run, then it stops. In addition it can also wait for other events. Each of these states has an equally intuitive name.

The actions done to move a process between these states also have special, harder to memorize, names that we have to learn because it makes it easier to communicate what the OS is doing to people.

Process control block

This is a container of information (data structure) used by the kernel to mange the processes. It contains all the information you could possibly need to know about it. Such as:

Context switching

If you want processes to run in parallel you need to let them share the CPU space at the same time. Now this isn't possible, so we'll go with the next best thing: Context Switching.

This is where we periodically pause the execution of a process save all the necessary data needed for resuming to a PCB in the OS, then later after the other processes have had their turn, we resume it using the stored PCB data.

Note: This is time consuming and so it's why pc's have multiple cores so that processes can run in parallel for real without this added overhead.

What is Context Switching in Operating System?

TOPIC 2 - Fundamental problems

Introduction to problems in Computer Science

What is a computer science problem?

Unlike the more traditional subjects, Computer science problems are all about what method is used to achieve a goal and how good of a method it is, rather than just can you achieve the goal. 

Problem in computer science are defined by:

The essential notions of computer science

Computer science at it's core really just boils down to doing Computations with limited Resources and with Correctness

In most contexts Resources are the time or memory used by the computations and uses, and Correctness refers to if the solution is actually answers the problem (or in the case of optimisation: if it is the best answer to the problem)

Abstraction

This is where you take a real world problem and transform it into an idealised (mathematical) version which can be understood by a computer but also where the solution to this new variation can still be used to give the answer to the original real world problem.

Abstracting the travelling salesman problem

To understand abstraction a bit better lets take a look at how we can apply it to the travelling salesman problem (TSP).

The TSP is where a salesman wishes to find the shortest tour of a given collection of cities. (So that he saves as much time/fuel as possible.) To solve this we need to find a way of abstracting the real world problem into something we can obtain a computational solution for. We can do this with the following steps

StepsTSP example
Identify problemFind shortest tour (by time) of a given collection of cities.
identify core information needed
  • Start city
  • Number of cities
  • Distance between cities
Create initial abstractionRepresent cities and distances as a weighted graph then the problem is equivalent finding the shortest hamolian cycle.
identify other considerations and abstractions for them
  • Cities not known until visited
    • Use queue of incoming cities
  • If distance measured in time then it might vary with traffic or if distance is euclidean distance, actual time taken may vary.
    • Represent distance as a function
  • What if some city-to.city trips aren't possible.
    • Represent distance as infinity.
Consider trade off between quality and cost of obtaining solutionA solution which does not take into account the above considerations may be computable faster but may be less relevant to the real-world problem.

Types of problems in computer science

Decision problems

This is a problem which asks a question about a given thing and the answer is either yes or no.

Common features:

Examples of decision problems and their abstractions

Real-world problemDescription of problemAbstractionOther applications
Map colouringGiven a plane map of regions with each contained within a continuous border can you colour the map with k number of colours where no two adjacent regions have the same colour?

Since all the information is contained within which regions touch each other we can abstract it as a graph colouring problem

Graph G = (V,E) where v is  a set of vertices and E is a set of connecting edges. Colour the graph such that all pair of vertices joined by an edge are coloured differently.

Scheduling, register allocation, radio frequency assignment
DraughtsGiven an n x n board of Draughts and a legal position of white and black checkers, where you play black and it's my go, can you still win from that position?Abstract as a tree of all possible future positions, and determine if at least one leaf node is white. (Aka a win by white)Chess, Go

 

Search problems

This is a problem where you have lots of possible solutions for a given input and you need to return one. If there is no solution you need to say so.

Common features:

To solve we a search where we are given an instance we need to find a solution from the possible solutions that fits the search relation for that instance. (or in plain english the solution needs to fit the criteria). Else there is no solution and we return "False".

Examples of search problems and their abstractions

Real-world problemDescription of problemAbstractionOther applications
Sorting listsGiven a list of items like name or numbers where some items may be repeated, give one way of sorting them so they are in some fixed ordering. (e.g numeric or alphanumeric)

Abstract to a list of symbols. However, must consider that in the case of lists of lists, the data structures will be more complex

Telephone directory, File structure

Finding routesGiven a map and two locations can you find a route from point A to B, while also avoiding motorways?

Abstract to:

  • A graph where vertices are towns on the map and the edges are routes between towns which don't pass through other towns. 
  • List of motorways.
  • Start and end towns
Chess, Go

Optimisation problems

These are problems where you have lots of possible solutions for a given input and you need to return the best one according to some criteria.

Common features:

Examples of optimisation problems and their abstractions

Real-world problemDescription of problemAbstractionOther applications
Travelling salesman problemGiven a collection of cities and the distance between pairs of cities find the length of the shortest tour of cities where each visited exactly once.

Abstract to a weighted graph.

(use abstraction described in detail earlier)

Telephone directory, File structure

Sports day

Given sports day events that must be scheduled at various times throughout the day, where each event takes 30 minutes and certain events can occur at the same time as long as the athletes are available what is the earliest time sports day can finish if there is the maximum number of events in the noon slot

Abstract to:

  • Graph where vertices are events and edges are events that cannot occur at the same time.
  • Noon slot will have the largest group of vertices where no vertice is joined by an edge with any other vertice.
  • The remaining slots are found by finding the minimal colouring of the remaining vertices, and assigning each colour to be a slot.
Scheduling

Methods to Solve Some Fundamental Problems

After abstracting a problem we then need an algorithm to solve that problem. We'll look at some different algorithms which exist for some of the fundamental computer science problems.

Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.

Greedy algorithm: An algorithm which makes choices based on what look best in the moment. (finds the local optimum but not the global)

Colouring maps (Graph colouring)

We can abstract this as a Graph colouring problem, where the vertices are regions of the map and edges are adjacent regions. 

We'll look at two different algorithms greedy colouring and point removal (brute force)

Sports day (Maximum independent set + Graph colouring)

We abstract this as a graph where the vertices are the events and events which conflict are connected by edges. Then the largest number of events which can scheduled in one slot is equivalent to finding the maximum independent set of the vertices. To find a way of grouping the remaining events into slots we can treat it as a vertex graph colouring problem, and then assign each colour to a slot.

Note: Maximum ≠ maximal. Maximal independent set is a set which is not contained in any other set. However there may still be large independent sets, such as the maximum.

We'll look at a heuristic greedy algorithm for findinging a large independent set.

  1. Choose the vertex of smallest degree and put it into the independent set
  2. Remove it and its neighbours from the graph
  3. Go through the remaining vertices repeating the process until no vertex remains.

This algorithm works on the heuristic principle that if you remove as few vertices as possible at each step, you'll be able to create a large independent set. However whilst this will produce a large independent set it unfortunately does not produce the largest independent set. So it is still just a greedy algorithm

Finding tours in the TSP 

We abstract the TSP as a weighted graph, and proceed to find the shortest cycle where each node is visited once. (Hamiltonian cycle)

A short but not optimally short path can be found with a greedy algorithm:

  1. Start at a random city.
  2. Pick the nearest unvisited city. (neighbour node with lowest edge weight.)
  3. Repeat until visited every city
  4. Output resulting tour.

Describing Algorithms

In Order to allow algorithms to be able to be described in a way which is sufficiently precise so that it can be programmed and analysed but still flexible enough that it can be used in any programming language, we will write them in a special language called Pseduo-code.

When it comes to algorithms we may be interested in proving:

Programming languages and paradigms

Programming languages vs algorithms

A programming language provides a way of translating an algorithm into a form, called a program, which can be executed by the computer.

Note: Algorithm ≠ Computer program. Whilst a program is concertely and explicitly defined in the programming language the algorithm can have many different implementations. Not only are there different implementations for each programming language but even with the same one there different implementations.

Programming paradigms

Programming paradigm: a way to classify programming languages based on their features.

There are lots and lots of paradigms, that people have come up with to group languages together but we'll just look at the most common.

 

Why are programming languages evolving?

Aliasing: Situation where the same memory location can be accessed by different pointers (names). Some people think this is a dangerous feature

Syntax and semantics

Syntax: Rules which tell us what can or can't be written in a programming language.

Semantics: Proving with formality what a program means.

Formally defining the semantics of a programming language should be done for the following reasons:

If we define semantics informally (like C, Fortran, python and many languages do) we can cause ambiguity which can cause errors.

There are different types of formal semantics we can use:

Compilers

Compilation vs interpretation

Compiled languages: The whole language is compiled at once into machine code.

Interpreted languages: Only one instruction at a time is translated to machine code and this is done during runtime.

Advantages of Compiled languagesAdvantages of Interpreted languages
Runs faster because not compiling during runtime.Uses memory better since whole code does not need compiling
Can analyse code and optimise before runtime.Faster development since there is zero compilation time.
Can avoid some runtime errors by finding bugs before runtime 

Hybrid languages are also possible which combine compilation and interpretation to get the best of both worlds.

How does compilation work?

  1. Lexical analyser - Reads program and converts the characters to a sequence of basic components called tokens.
  2. Syntax analyser -  represents the syntactic structure of the tokens as a parse tree.
  3. Translation - Flattens the parse tree into intermediate code (e.g bytecode), this is similar to assembly.
  4. Code generation - Translates the intermediate code into assembly and then into machine code.

Phases of Compiler with Example

Lexical Analysis and Regular expressions

Regular expression: An expression which selects characters in the text from the alphabet \sum

Some examples of regular expressions:

Theorem: A string is denoted by a regular express iff it is accepted by a final state machine

Finite state machines

A finite state machine is a mathematical model of computation which can be in one of a finite number of states and can have it's state changed by inputs. 

A finite state machine has:

Pictorial representation of an FSM:

TOPIC 3 - Efficiency and complexity

Analysis of algorithms

Resources used by algorithms

Measuring the time taken by an algorithm

We measure time taken by algorithm rather than measure time taken by the implementation because this way it doesn't vary according to the programming language, computer or the processor speed. Thus it is machine-independent

Fundamental assumptions of algorithms:

basic operation: An operation that takes one FDFE cycle.

Time complexity

Worst-case time complexity: This is given as a function of n, the input size, and tells how many units of time c, it would take to run the algorithm in the worst case. It is expressed as a function because if it was a constant then no matter what we picked we could find a value of n, for which the time surpassed that bound.

Big O notation: This lets us denote the worst-case time complexity and give an upper bound on the time.

Question

Define precisely what we mean when we say that two functions  f (n) : N N and g(n) : N N are such that f = O(g).   [2]

Complexity and hardness

Complexity classes compared

Efficiently checkable/solvable: Can be checked/solved in polynomial time

Polynomial-time reduction: A polynomial time algorithm to convert one decision problem into another. where if one is true the other must also be true.

SymbolComplexity classMeaningExamples
P(Deterministic) polynomial timesolvable in polynomial time. (tractable)Sorting lists
NPNon-deterministic polynomial timeCheckable in polynomial time, but not solvable (intractable)
  • Decision version  TSP
  • Decision version graph colouring
  • Decision version independent set 
NP-completeA problem which can be reduced through a polynomial-time reduction into any  NP problem and is in NP
  • Decision version  TSP
NP-hardA problem which can be reduced into any NP problem with a polynomial-time reduction. (NP complete is a subset of NP-hard)
  • optimisation version of TSP

 

NP (complexity) - Wikipedia

P = NP

This is a famous unsolved computer science problem which attempts to show if a problem can be checked in polynomial time to can be solved in polynomial time. Suspected to be false.

Dealing with NP-hardness

Brute force

This is the idea of relying on computing power instead of intelligence by using a naive method to find all possible instances and pick the best/correct one.

Heuristic methods

Editors

View count: 680